package com.hyj.serve.manage.entity.firewall;

import cn.hutool.core.collection.CollUtil;
import cn.hutool.core.util.StrUtil;
import lombok.Data;
import org.dom4j.Document;
import org.dom4j.DocumentException;
import org.dom4j.DocumentHelper;
import org.dom4j.Element;

import java.util.ArrayList;
import java.util.List;

/**
 * @author HeYuJie
 * @date 2022/6/6
 */
@Data
public class FirewallZone extends RuleItem {

    public static final String TAG_NAME = "zone";

    private String type = TAG_NAME;

    private String short_;
    private String description = "CentosManage工具管理";

    private List<RuleItem> rules;

    @Override
    public String serializeXml(String prefix) {
        StringBuilder sb = new StringBuilder();
        sb.append("<?xml version=\"1.0\" encoding=\"utf-8\"?>\n");
        sb.append("<"+TAG_NAME+">\n");
        sb.append(prefix).append("  <short>").append(this.short_).append("</short>\n");
        sb.append(prefix).append("  <description>").append(this.description).append("</description>\n");

        if(CollUtil.isNotEmpty(rules)){
            for (RuleItem rule : rules) {
                sb.append(rule.serializeXml(prefix+"  ")).append("\n");
            }
        }

        return sb.append("</"+ TAG_NAME +">").toString();
    }

    public static FirewallZone deserializeXml(String xml){
        if(StrUtil.isEmpty(xml)){
            return null;
        }
        try {
            Document document = DocumentHelper.parseText(xml);
            Element rootElement = document.getRootElement();
            return deserializeXml(rootElement);
        } catch (DocumentException e) {
            e.printStackTrace();
        }
        return null;
    }

    public static FirewallZone deserializeXml(Element xml){
        if(xml == null || !xml.getName().equalsIgnoreCase(TAG_NAME)){
            return null;
        }

        FirewallZone zone = new FirewallZone();

        List<Element> elements = xml.elements();

        List<RuleItem> rules = new ArrayList<>(elements.size());
        zone.rules = rules;

        for (Element element : elements) {

            String name = element.getName();
            if(name.equals("short")){
                zone.short_ = element.getTextTrim();
            } else if(name.equals("description")){
//                zone.description = element.getTextTrim();

            } else if(name.equals(FirewallRule.TAG_NAME)){
                FirewallRule rule = FirewallRule.deserializeXml(element);
                if(rule != null){
                    rules.add(rule);
                }

            } else if(name.equals(PortRule.TAG_NAME)){
                PortRule rule = PortRule.deserializeXml(element);
                if(rule != null){
                    rules.add(rule);
                }
            } else if(name.equals(ServiceRule.TAG_NAME)){
                ServiceRule rule = ServiceRule.deserializeXml(element);
                if(rule != null){
                    rules.add(rule);
                }
            } else if(name.equals(SourceRule.TAG_NAME)){
                SourceRule rule = SourceRule.deserializeXml(element);
                if(rule != null){
                    rules.add(rule);
                }
            }
        }

        return zone;
    }

}
